package com.transaction.manage.service;

import com.transaction.common.base.User;
import com.transaction.common.util.MD5Util;
import com.transaction.manage.localMapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    public User loadUserByUsername(String accountName, String passWord){
        User select = new User();
        select.setAccountName(accountName);
        User user =  userMapper.selectOne(select);
        if(Objects.nonNull(user)){
            //比对密码
            String pass = user.getPassWord();
            String salt = user.getSalt();
            String mdPass = MD5Util.encryptedPassword(accountName,passWord,salt);
            if(!pass.equals(mdPass)){
                throw new RuntimeException("密码错误!");
            }
        }else{
            throw new RuntimeException("用户名不存在!");
        }
        return user;
    }
}
